using Senparc.Weixin.Context;
using Senparc.Weixin.Exceptions;
using Senparc.Weixin.MessageHandlers;
using Senparc.Weixin.QY.Entities;
using Senparc.Weixin.QY.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using Tencent;
namespace Senparc.Weixin.QY.MessageHandlers
{
	public abstract class QyMessageHandler<TC> : MessageHandler<TC, IRequestMessageBase, IResponseMessageBase>, IQyMessageHandler, IMessageHandler<IRequestMessageBase, IResponseMessageBase>, IMessageHandlerDocument where TC : class, IMessageContext<IRequestMessageBase, IResponseMessageBase>, new()
	{
		public static WeixinContext<TC, IRequestMessageBase, IResponseMessageBase> GlobalWeixinContext = new WeixinContext<TC, IRequestMessageBase, IResponseMessageBase>();
		private PostModel _postModel;
		public override WeixinContext<TC, IRequestMessageBase, IResponseMessageBase> WeixinContext
		{
			get
			{
				return QyMessageHandler<TC>.GlobalWeixinContext;
			}
		}
		public override XDocument ResponseDocument
		{
			get
			{
				if (this.ResponseMessage == null)
				{
					return null;
				}
				return (this.ResponseMessage as ResponseMessageBase).ConvertEntityToXml<ResponseMessageBase>();
			}
		}
		public override XDocument FinalResponseDocument
		{
			get
			{
				if (this.ResponseDocument == null)
				{
					return null;
				}
				string sTimeStamp = DateTime.Now.Ticks.ToString();
				string sNonce = DateTime.Now.Ticks.ToString();
				WXBizMsgCrypt wXBizMsgCrypt = new WXBizMsgCrypt(this._postModel.Token, this._postModel.EncodingAESKey, this._postModel.CorpId);
				string text = null;
				wXBizMsgCrypt.EncryptMsg(this.ResponseDocument.ToString(), sTimeStamp, sNonce, ref text);
				return XDocument.Parse(text);
			}
		}
		public int AgentId
		{
			get
			{
				if (this.EncryptPostData == null)
				{
					return -1;
				}
				return this.EncryptPostData.AgentID;
			}
		}
		public EncryptPostData EncryptPostData
		{
			get;
			set;
		}
		public new IRequestMessageBase RequestMessage
		{
			get
			{
				return base.RequestMessage;
			}
			set
			{
				base.RequestMessage = value;
			}
		}
		public new IResponseMessageBase ResponseMessage
		{
			get
			{
				return base.ResponseMessage;
			}
			set
			{
				base.ResponseMessage = value;
			}
		}
		public QyMessageHandler(Stream inputStream, PostModel postModel, int maxRecordCount = 0) : base(inputStream, maxRecordCount, postModel)
		{
		}
		public QyMessageHandler(XDocument requestDocument, PostModel postModel, int maxRecordCount = 0) : base(requestDocument, maxRecordCount, postModel)
		{
		}
		public override XDocument Init(XDocument postDataDocument, object postData)
		{
			this._postModel = (postData as PostModel);
			string text = postDataDocument.ToString();
			this.EncryptPostData = RequestMessageFactory.GetEncryptPostData(text);
			WXBizMsgCrypt wXBizMsgCrypt = new WXBizMsgCrypt(this._postModel.Token, this._postModel.EncodingAESKey, this._postModel.CorpId);
			string text2 = null;
			int num = wXBizMsgCrypt.DecryptMsg(this._postModel.Msg_Signature, this._postModel.Timestamp, this._postModel.Nonce, text, ref text2);
			if (num != 0)
			{
				base.CancelExcute = true;
				return null;
			}
			XDocument xDocument = XDocument.Parse(text2);
			this.RequestMessage = RequestMessageFactory.GetRequestEntity(xDocument);
			if (WeixinContextGlobal.UseWeixinContext)
			{
				this.WeixinContext.InsertMessage(this.RequestMessage);
			}
			return xDocument;
		}
		public TR CreateResponseMessage<TR>() where TR : ResponseMessageBase
		{
			if (this.RequestMessage == null)
			{
				return default(TR);
			}
			return this.RequestMessage.CreateResponseMessage<TR>();
		}
		public override void Execute()
		{
			if (base.CancelExcute)
			{
				return;
			}
			this.OnExecuting();
			if (base.CancelExcute)
			{
				return;
			}
			try
			{
				if (this.RequestMessage != null)
				{
					switch (this.RequestMessage.MsgType)
					{
					case RequestMsgType.DEFAULT:
					{
						if (!(this.RequestMessage is IThirdPartyInfoBase))
						{
							throw new WeixinException("没有找到合适的消息类型。");
						}
						IThirdPartyInfoBase thirdPartyInfoBase = this.RequestMessage as IThirdPartyInfoBase;
						switch (thirdPartyInfoBase.InfoType)
						{
						case ThirdPartyInfo.SUITE_TICKET:
						case ThirdPartyInfo.CHANGE_AUTH:
						case ThirdPartyInfo.CANCEL_AUTH:
							base.TextResponseMessage = "success";
							goto IL_1AA;
						default:
							throw new UnknownRequestMsgTypeException("未知的InfoType请求类型", null);
						}
						break;
					}
					case RequestMsgType.Text:
					{
						RequestMessageText requestMessage = this.RequestMessage as RequestMessageText;
						this.ResponseMessage = (this.OnTextOrEventRequest(requestMessage) ?? this.OnTextRequest(requestMessage));
						goto IL_1AA;
					}
					case RequestMsgType.Location:
						this.ResponseMessage = this.OnLocationRequest(this.RequestMessage as RequestMessageLocation);
						goto IL_1AA;
					case RequestMsgType.Image:
						this.ResponseMessage = this.OnImageRequest(this.RequestMessage as RequestMessageImage);
						goto IL_1AA;
					case RequestMsgType.Voice:
						this.ResponseMessage = this.OnVoiceRequest(this.RequestMessage as RequestMessageVoice);
						goto IL_1AA;
					case RequestMsgType.Video:
						this.ResponseMessage = this.OnVideoRequest(this.RequestMessage as RequestMessageVideo);
						goto IL_1AA;
					case RequestMsgType.Event:
					{
						RequestMessageText requestMessage2 = (this.RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText();
						this.ResponseMessage = (this.OnTextOrEventRequest(requestMessage2) ?? this.OnEventRequest(this.RequestMessage as IRequestMessageEventBase));
						goto IL_1AA;
					}
					case RequestMsgType.ShortVideo:
						this.ResponseMessage = this.OnShortVideoRequest(this.RequestMessage as RequestMessageShortVideo);
						goto IL_1AA;
					}
					throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null);
					IL_1AA:
					if (WeixinContextGlobal.UseWeixinContext && this.ResponseMessage != null)
					{
						this.WeixinContext.InsertMessage(this.ResponseMessage);
					}
				}
			}
			catch (Exception ex)
			{
				throw new MessageHandlerException("MessageHandler中Execute()过程发生错误：" + ex.Message, ex);
			}
			finally
			{
				this.OnExecuted();
			}
		}
		public new virtual void OnExecuting()
		{
			if (base.OmitRepeatedMessage)
			{
				TC currentMessageContext = this.CurrentMessageContext;
				if (currentMessageContext.RequestMessages.Count > 1)
				{
					TC currentMessageContext2 = this.CurrentMessageContext;
					List<IRequestMessageBase> arg_59_0 = currentMessageContext2.RequestMessages;
					TC currentMessageContext3 = this.CurrentMessageContext;
					IRequestMessageBase requestMessageBase = arg_59_0[currentMessageContext3.RequestMessages.Count - 2];
					if ((requestMessageBase.MsgId != 0L && requestMessageBase.MsgId == this.RequestMessage.MsgId) || (requestMessageBase.CreateTime == this.RequestMessage.CreateTime && requestMessageBase.MsgType != this.RequestMessage.MsgType))
					{
						base.CancelExcute = true;
						return;
					}
				}
			}
			base.OnExecuting();
		}
		public new virtual void OnExecuted()
		{
			base.OnExecuted();
		}
		public abstract IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage);
		public virtual IResponseMessageBase OnTextOrEventRequest(RequestMessageText requestMessage)
		{
			return null;
		}
		public virtual IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnVideoRequest(RequestMessageVideo requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnShortVideoRequest(RequestMessageShortVideo requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEventRequest(IRequestMessageEventBase requestMessage)
		{
			IRequestMessageEventBase requestMessageEventBase = this.RequestMessage as IRequestMessageEventBase;
			switch (requestMessageEventBase.Event)
			{
			case Event.LOCATION:
			{
				IResponseMessageBase result = this.OnEvent_LocationRequest(this.RequestMessage as RequestMessageEvent_Location);
				return result;
			}
			case Event.subscribe:
			{
				IResponseMessageBase result = this.OnEvent_SubscribeRequest(this.RequestMessage as RequestMessageEvent_Subscribe);
				return result;
			}
			case Event.unsubscribe:
			{
				IResponseMessageBase result = this.OnEvent_UnSubscribeRequest(this.RequestMessage as RequestMessageEvent_UnSubscribe);
				return result;
			}
			case Event.CLICK:
			{
				IResponseMessageBase result = this.OnEvent_ClickRequest(this.RequestMessage as RequestMessageEvent_Click);
				return result;
			}
			case Event.VIEW:
			{
				IResponseMessageBase result = this.OnEvent_ViewRequest(this.RequestMessage as RequestMessageEvent_View);
				return result;
			}
			case Event.SCANCODE_PUSH:
			{
				IResponseMessageBase result = this.OnEvent_ScancodePushRequest(this.RequestMessage as RequestMessageEvent_Scancode_Push);
				return result;
			}
			case Event.SCANCODE_WAITMSG:
			{
				IResponseMessageBase result = this.OnEvent_ScancodeWaitmsgRequest(this.RequestMessage as RequestMessageEvent_Scancode_Waitmsg);
				return result;
			}
			case Event.PIC_SYSPHOTO:
			{
				IResponseMessageBase result = this.OnEvent_PicSysphotoRequest(this.RequestMessage as RequestMessageEvent_Pic_Sysphoto);
				return result;
			}
			case Event.PIC_PHOTO_OR_ALBUM:
			{
				IResponseMessageBase result = this.OnEvent_PicPhotoOrAlbumRequest(this.RequestMessage as RequestMessageEvent_Pic_Photo_Or_Album);
				return result;
			}
			case Event.PIC_WEIXIN:
			{
				IResponseMessageBase result = this.OnEvent_PicWeixinRequest(this.RequestMessage as RequestMessageEvent_Pic_Weixin);
				return result;
			}
			case Event.LOCATION_SELECT:
			{
				IResponseMessageBase result = this.OnEvent_LocationSelectRequest(this.RequestMessage as RequestMessageEvent_Location_Select);
				return result;
			}
			case Event.ENTER_AGENT:
			{
				IResponseMessageBase result = this.OnEvent_EnterAgentRequest(this.RequestMessage as RequestMessageEvent_Enter_Agent);
				return result;
			}
			case Event.BATCH_JOB_RESULT:
			{
				IResponseMessageBase result = this.OnEvent_BatchJobResultRequest(this.RequestMessage as RequestMessageEvent_Batch_Job_Result);
				return result;
			}
			}
			throw new UnknownRequestMsgTypeException("未知的Event下属请求信息", null);
		}
		public virtual IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_ViewRequest(RequestMessageEvent_View requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_PicPhotoOrAlbumRequest(RequestMessageEvent_Pic_Photo_Or_Album requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_ScancodePushRequest(RequestMessageEvent_Scancode_Push requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_ScancodeWaitmsgRequest(RequestMessageEvent_Scancode_Waitmsg requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_LocationSelectRequest(RequestMessageEvent_Location_Select requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_PicWeixinRequest(RequestMessageEvent_Pic_Weixin requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_PicSysphotoRequest(RequestMessageEvent_Pic_Sysphoto requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_UnSubscribeRequest(RequestMessageEvent_UnSubscribe requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_LocationRequest(RequestMessageEvent_Location requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_EnterAgentRequest(RequestMessageEvent_Enter_Agent requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
		public virtual IResponseMessageBase OnEvent_BatchJobResultRequest(RequestMessageEvent_Batch_Job_Result requestMessage)
		{
			return this.DefaultResponseMessage(requestMessage);
		}
	}
}
